﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace YunShop_WebAPI.BLL
{
    public class MniMngService
    {
        public static string HttpPostData(string url, string param)
        {
            var result = string.Empty;
            //注意提交的编码 这边是需要改变的 这边默认的是Default：系统当前编码
            var postData = Encoding.UTF8.GetBytes(param);

            // 设置提交的相关参数             
            HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
            Encoding myEncoding = Encoding.UTF8;
            request.Method = "POST";
            request.KeepAlive = false;
            request.ProtocolVersion = HttpVersion.Version10;
            request.AllowAutoRedirect = true;
            request.ContentType = "application/x-www-form-urlencoded";
            request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR  3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)";
            request.ContentLength = postData.Length;

            // 提交请求数据 
            System.IO.Stream outputStream = request.GetRequestStream();
            outputStream.Write(postData, 0, postData.Length);
            outputStream.Close();

            HttpWebResponse response;
            Stream responseStream;
            StreamReader reader;
            string srcString;
            response = request.GetResponse() as HttpWebResponse;
            responseStream = response.GetResponseStream();
            reader = new System.IO.StreamReader(responseStream, Encoding.GetEncoding("UTF-8"));
            srcString = reader.ReadToEnd();
            result = srcString;   //返回值赋值
            reader.Close();
            return result;
        }

        public static string GoodsModelToStr(Model.GoodsModel goodsModel)
        {
            ArrayList list = new ArrayList();
            Hashtable ht = new Hashtable();
            try
            {
                Type type = typeof(Model.GoodsModel);
                PropertyInfo[] properties = type.GetProperties();
                foreach (PropertyInfo prop in properties)
                {
                    string sName = prop.Name;
                    string sValue = prop.GetValue(goodsModel).ToString();
                    ht.Add(sName, sValue);
                }
                list.Add(ht);
            }
            catch { }
            string myjson = PluSoft.Utils.JSON.Encode(list);
            return myjson;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="gtypeModel"></param>
        /// <param name="ret_type">0返回ArrayList，1返回Hashtable</param>
        /// <returns></returns>
        public static string GoodsTypeModelToStr(Model.GoodsTypeModel gtypeModel,int ret_type)
        {
            ArrayList list = new ArrayList();
            Hashtable ht = new Hashtable();
            try
            {
                Type type = typeof(Model.GoodsTypeModel);
                PropertyInfo[] properties = type.GetProperties();
                foreach (PropertyInfo prop in properties)
                {
                    string sName = prop.Name;
                    string sValue = prop.GetValue(gtypeModel).ToString();
                    ht.Add(sName, sValue);
                }
                list.Add(ht);
            }
            catch { }
            if (ret_type == 1) return PluSoft.Utils.JSON.Encode(ht);
            string myjson = PluSoft.Utils.JSON.Encode(list);
            return myjson;
        }

        public static string BaseInfoModelToStr(Model.BaseInfoModel baseInfoModel, int ret_type)
        {
            ArrayList list = new ArrayList();
            Hashtable ht = new Hashtable();
            try
            {
                Type type = typeof(Model.BaseInfoModel);
                PropertyInfo[] properties = type.GetProperties();
                foreach (PropertyInfo prop in properties)
                {
                    string sName = prop.Name;
                    string sValue = prop.GetValue(baseInfoModel).ToString();
                    ht.Add(sName, sValue);
                }
                list.Add(ht);
            }
            catch { }
            if (ret_type == 1) return PluSoft.Utils.JSON.Encode(ht);
            string myjson = PluSoft.Utils.JSON.Encode(list);
            return myjson;
        }

        public static string TimeInfoModelToStr(Model.TimeInfoModel timeInfoModel)
        {
            Hashtable ht = new Hashtable();
            try
            {
                Type type = typeof(Model.TimeInfoModel);
                PropertyInfo[] properties = type.GetProperties();
                foreach (PropertyInfo prop in properties)
                {
                    string sName = prop.Name;
                    string sValue = prop.GetValue(timeInfoModel).ToString();
                    ht.Add(sName, sValue);
                }
            }
            catch { }
            return PluSoft.Utils.JSON.Encode(ht);
        }

        public static string SupplierModelToStr(Model.SupplierModel supplierModel)
        {
            ArrayList list = new ArrayList();
            Hashtable ht = new Hashtable();
            try
            {
                Type type = typeof(Model.SupplierModel);
                PropertyInfo[] properties = type.GetProperties();
                foreach (PropertyInfo prop in properties)
                {
                    string sName = prop.Name;
                    string sValue = prop.GetValue(supplierModel).ToString();
                    ht.Add(sName, sValue);
                }
                list.Add(ht);
            }
            catch { }
            string myjson = PluSoft.Utils.JSON.Encode(list);
            return myjson;
        }

        public static string MemberTypeModelToStr(Model.MemberTypeModel mtypeModel)
        {
            ArrayList list = new ArrayList();
            Hashtable ht = new Hashtable();
            try
            {
                Type type = typeof(Model.MemberTypeModel);
                PropertyInfo[] properties = type.GetProperties();
                foreach (PropertyInfo prop in properties)
                {
                    string sName = prop.Name;
                    string sValue = prop.GetValue(mtypeModel).ToString();
                    ht.Add(sName, sValue);
                }
                list.Add(ht);
            }
            catch { }
            string myjson = PluSoft.Utils.JSON.Encode(list);
            return myjson;
        }

        public static string MemberModelToStr(Model.MemberModel memberModel)
        {
            try
            {
                ArrayList list = new ArrayList();
                Hashtable ht = new Hashtable();
                try
                {
                    Type type = typeof(Model.MemberModel);
                    PropertyInfo[] properties = type.GetProperties();
                    foreach (PropertyInfo prop in properties)
                    {
                        string sName = prop.Name;
                        string sValue = prop.GetValue(memberModel).ToString();
                        ht.Add(sName, sValue);
                    }
                    list.Add(ht);
                }
                catch (Exception ex)
                {

                }
                string myjson = PluSoft.Utils.JSON.Encode(list);
                return myjson;
            }
            catch (Exception ex)
            {
                return "";
            }
        }

        public static string UserModelToStr(Model.UserModel userModel)
        {
            try
            {
                ArrayList list = new ArrayList();
                Hashtable ht = new Hashtable();
                try
                {
                    Type type = typeof(Model.UserModel);
                    PropertyInfo[] properties = type.GetProperties();
                    foreach (PropertyInfo prop in properties)
                    {
                        string sName = prop.Name;
                        string sValue = prop.GetValue(userModel).ToString();
                        ht.Add(sName, sValue);
                    }
                    list.Add(ht);
                }
                catch (Exception ex)
                {

                }
                string myjson = PluSoft.Utils.JSON.Encode(list);
                return myjson;
            }
            catch (Exception ex)
            {
                return "";
            }
        }

        public static string UserPowerModelToStr(Model.UserPowerModel userPower)
        {
            try
            {
                Hashtable ht = new Hashtable();
                try
                {
                    Type type = typeof(Model.UserPowerModel);
                    PropertyInfo[] properties = type.GetProperties();
                    foreach (PropertyInfo prop in properties)
                    {
                        string sName = prop.Name;
                        string sValue = prop.GetValue(userPower).ToString();
                        ht.Add(sName, sValue);
                    }
                }
                catch (Exception ex)
                {

                }
                string myjson = PluSoft.Utils.JSON.Encode(ht);
                return myjson;
            }
            catch (Exception ex)
            {
                return "";
            }
        }

        public static string EmployeeModelToStr(Model.EmployeeModel empModel)
        {
            try
            {
                ArrayList list = new ArrayList();
                Hashtable ht = new Hashtable();
                try
                {
                    Type type = typeof(Model.EmployeeModel);
                    PropertyInfo[] properties = type.GetProperties();
                    foreach (PropertyInfo prop in properties)
                    {
                        string sName = prop.Name;
                        string sValue = prop.GetValue(empModel).ToString();
                        ht.Add(sName, sValue);
                    }
                    list.Add(ht);
                }
                catch (Exception ex)
                {

                }
                string myjson = PluSoft.Utils.JSON.Encode(list);
                return myjson;
            }
            catch (Exception ex)
            {
                return "";
            }
        }

        public static string GetCheckBillJson(string shop_appid,int sDataType, string connstr)
        {
            try
            {
                Int64 mStock = SystemInfo.GetSystemAppID(shop_appid, SystemAppID.StockID);
                string msql = "select KInterID From y_Stc_Master where FStockID = {0} and FCheckFlag = 0";
                msql = string.Format(msql, mStock);
                ArrayList datalist = DBHelper.Select(msql, connstr);
                if (datalist.Count == 0) return "";
                ArrayList list = new ArrayList();
                Hashtable ht = new Hashtable();
                try
                {                                    
                    ht.Add("FSystemAppID", shop_appid);
                    ht.Add("FStockID", mStock);
                    ht.Add("FDataType", sDataType);
                    ht.Add("bill_list", datalist);
                    list.Add(ht);
                }
                catch (Exception ex)
                {

                }
                string myjson = PluSoft.Utils.JSON.Encode(list);
                return myjson;
            }
            catch (Exception ex)
            {
                return "";
            }
        }

        public static Int64 GetEmployeeID(string shop_appid)
        {
            try
            {
                int mCorpID = SystemInfo.GetSystemAppID(shop_appid, SystemAppID.CorpID);
                Int64 mUserID = SystemInfo.GetSystemAppID(shop_appid, SystemAppID.UserID);
                string connstr = SystemInfo.GetClusterConn(mCorpID); 
                string msql = "select FItemID From t_Employee where FUserID = {0}";
                msql = string.Format(msql, mUserID);
                DataTable dt = DBHelper.GetDataTable(msql,connstr);
                if (dt.Rows.Count == 0) return 1;
                return CommonFun.ConvertToBigInt(dt.Rows[0]["FItemID"]);
            }
            catch (Exception ex)
            {
                return 1;
            }
        }

    }
}
